package kt.dal;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import kt.util.StringUtil;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class DBPoolXData {
	private static Logger logger = LogManager.getLogger(DBPoolX.class);
//	private static final String fileName = "conf/dbpool.conf";
	private ArrayList<String> poolnames;
	private ConnectionParameter parameter;
	private static ArrayList<DBPoolXData> poolDataList = new ArrayList<DBPoolXData>();

	private static boolean loaded = false;

	public static ArrayList<DBPoolXData> getPoolDataList() {
		return poolDataList;
	}

	public ConnectionParameter getParameter() {
		return this.parameter;
	}

	public void setParameter(ConnectionParameter parameter) {
		this.parameter = parameter;
	}

	public ArrayList<String> getPoolnames() {
		return this.poolnames;
	}

	public void setPoolnames(ArrayList<String> poolnames) {
		this.poolnames = poolnames;
	}

	public String toString() {
		return getPoolnames() + " = " + this.parameter.toString();
	}

	public static boolean isLoaded() {
		return loaded;
	}
		

	public static void loadData() {
		System.out.println("Begin load data");
		logger.info("Begin load data");
		ArrayList<DBPoolXData> v = new ArrayList<DBPoolXData>();
		try {
			logger.info("load config file " + "./conf/dbpool.conf");
			BufferedReader in = new BufferedReader(new FileReader("./conf/dbpool.conf"));
			String line = in.readLine();
			for (; line != null; line = in.readLine()) {
				line = line.trim();
				if (("".equals(line)) || (line.startsWith("#"))) {
					continue;
				}

				int idx = line.indexOf("=");
				if (idx <= 0) {
					System.out.println("   Seperator '=' is NOT found.");
				} else {
					String name = line.substring(0, idx).trim();
					String value = line.substring(idx + 1).trim();

					if ((name == null) || (name.equals("")) || (value == null)
							|| (value.equals(""))) {
						continue;
					}
					DBPoolXData data = new DBPoolXData();
					data.setPoolnames(StringUtil.parseString(name, ","));
					ConnectionParameter parameter = new ConnectionParameter();

					Collection<String> c = StringUtil.parseString(value, ",");
					if (c.size() < 5) {
						System.out.println("   Invalid DB parameter <driver>,<url>,<user><passEncrypted>,<count>: " + value);
						logger.error("   Invalid DB parameter <driver>,<url>,<user><passEncrypted>,<count>: " + value);
					} else {
						Iterator<String> it = c.iterator();
						parameter.setDriver((String) it.next());
						parameter.setUrl((String) it.next());
						parameter.setUser((String) it.next());
						String password = (String) it.next();
						try {
							parameter.setPassword(password);
						} catch (Exception ex) {
							System.out.println("   DBPoolXData: invalid encrypted password = " + password);
							ex.printStackTrace();
							logger.error("error while load the config file", ex);
						}
						try {
							parameter.setCount(Integer.parseInt((String) it
									.next()));
						} catch (Exception localException1) {
						}
						if (parameter.getCount() < 1)
							parameter.setCount(1);
						if (parameter.getCount() > 10)
							parameter.setCount(10);

						data.setParameter(parameter);
						System.out.println(data.toString());
						v.add(data);
					}
				}
			}
			loaded = true;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		poolDataList.clear();
		poolDataList.addAll(v);
		System.out.println("number of db connection: " + v.size());
		for (Object dbCnn : v) {
			System.out.println(dbCnn.toString());
		}
	}
}